/**
 * 通用uni-app网络请求
 * 基于 Promise 对象实现更简单的 request 使用方式，支持请求和响应拦截
 */
import Request from '@/uni_modules/uview-ui/libs/luch-request/index.js';

import tool from '../tool/tool'
import {
	LOGININFO_KEY,
	ACCESS_TOKEN_KEY,
	API_IP
} from "@/common/const/index.js";

export default {
	config: {
		// 请求的公共url
		baseUrl: API_IP,
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
			// 'Content-Type': 'application/x-www-form-urlencoded',
			// "cookie":"JSESSIONID=" + uni.getStorageSync("sessionid"),
		},
		data: {},
		method: "GET",
		dataType: "json",
		/* 如设为json，会对返回的数据做一次 JSON.parse */
		responseType: "text",
		success() {},
		fail() {},
		complete() {}
	},
	// 请求拦截器
	interceptor: {
		request: function(event) {},
		response: null
	},
	request(options) {
		if (!options) {
			options = {}
		}

		let token;
		let requestType = options.url.split('/')
		let typeVal = requestType[requestType.length - 1]

		// 判断筛选出以上三个页面不需要为请求头设置token，根据自己的项目情况而定
		if (typeVal == 'access_token') {
			delete this.config.header.Authorization
		} else {
		}

		// 请求服务ip
		let request_url = uni.getStorageSync('baseUrl');
		// 从服务器设置中读取ip
		if (request_url) {
			this.config.baseUrl = request_url
		}

		// 请求头中加上接口访问令牌
		let authorization = uni.getStorageSync(ACCESS_TOKEN_KEY);
		if (authorization) {
			options.header.Authorization = authorization;
		}
		
		options.baseUrl = options.baseUrl || this.config.baseUrl
		options.dataType = options.dataType || this.config.dataType
		delete options.dataType

		// 实际url
		options.url = options.baseUrl + options.url
		options.data = options.data || {}
		options.method = options.method || this.config.method

		// 基于 Promise 的网络请求
		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: '加载中...'
			})
			let _config = null
			options.complete = (response) => {
				uni.hideLoading();
				let statusCode = response.statusCode
				
				response.config = _config
				if (process.env.NODE_ENV === 'development') {
					if (statusCode === 200) {} else {}
				}
				
				if(statusCode === 404) {
					tool.msg('连接服务器失败，未找到后端服务器！');
					return
				}

				
				if (this.interceptor.response) {
					let newResponse = this.interceptor.response(response)
					if (newResponse) {
						response = newResponse
					}
				}
				let {
					Code,
					Message
				} = response.data
				
				if (response.statusCode == 403) {
					let {
						error
					} = response.data
					if (error && error.message){
						tool.msg(error.message)
						return
					}
				}
				
				/* if (response.statusCode == 401) {
					uni.removeStorageSync('SUID');
					uni.removeStorageSync('SNAME');
					uni.removeStorageSync('SFACE');
					uni.redirectTo({
						url: '/pages/sys/login/login'
					});
					return;
				} */
				
				if (response.statusCode == 401) {
					tool.msg(Message)
					/* setTimeout(() => {
						uni.reLaunch({
							url: '/pages/sys/login/login'
						})
					}, 1000) */
					reject(response)
					
					
				} else if (Code == 0) {
					tool.msg(Message)
					resolve(response.data)
				} else {
					resolve(response.data)
				}
			}
			_config = Object.assign({}, this.config, options)
			_config.requestId = new Date().getTime()

			if (this.interceptor.request) {
				this.interceptor.request(_config)
			}
			uni.request(_config);
		});
	},
	// get请求
	get(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	// post请求
	post(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	// put请求
	put(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	},
	// delete请求
	delete(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	}
}